package com.hand.wfive.dialog

import android.app.Dialog
import android.content.Context
import android.graphics.Point
import android.os.Bundle
import android.view.View
import com.hand.wfive.R
import kotlinx.android.synthetic.main.dialog_buy_exit_tips.*

class BuyExitTipsDialog(
    context: Context,
    private var mListener: VipTipListener?,
) : Dialog(
    context, R.style.CustomDialog) {

    companion object {
        fun showVipDialog(context: Context, listener: VipTipListener?) {
            BuyExitTipsDialog(context, listener).show()
        }
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.dialog_buy_exit_tips)
        setWidth()
        //按空白处不能取消动画
        setCanceledOnTouchOutside(false)
        setCancelable(false)
        //初始化界面控件的事件
        initEvent()
    }

    private fun setWidth() {
        //设置宽度
        val m = window?.windowManager
        val d = m?.defaultDisplay
        val p = window?.attributes
        val size = Point()
        d?.getSize(size)
        p?.width = (size.x * 0.75).toInt() //设置dialog的宽度为当前手机屏幕的宽度*0.8
        window?.attributes = p
    }

    /**
     * 初始化界面的确定和取消监听器
     */
    private fun initEvent() {
        //设置取消按钮被点击后，向外界提供监听
        ivBuy.setOnClickListener { v: View? ->
            mListener?.doBuy()
            dismiss()
        }
        tvClose.setOnClickListener { v: View? ->
            mListener?.doClose()
            dismiss()
        }
    }

    interface VipTipListener {
        fun doClose()
        fun doBuy(){}
    }
}